//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ParametricExerciseAdapter.h"
using namespace Cephei::QL::Models::Marketmodels::Callability;
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/Callability/MarketModelParametricExercise.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::CParametricExerciseAdapter (Cephei::QL::Models::Marketmodels::Callability::IMarketModelParametricExercise^ exercise, Cephei::IMatrix<Double>^ parameters) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CMarketModelParametricExercise^ _Cexercise;
    CoMatrix<Double>^ _Cparameters;
    try
    {
#ifdef HANDLE
        _phParametricExerciseAdapter = NULL;
#endif
        _Cexercise = safe_cast<CMarketModelParametricExercise^> (exercise);
        _Cexercise->Lock();
        QuantLib::MarketModelParametricExercise& _exercise = static_cast<QuantLib::MarketModelParametricExercise&> (_Cexercise->GetReference ()); 
        CoMatrix<Double>^ _Cparameters = safe_cast<CoMatrix<Double>^> (parameters);
        _Cparameters->Lock();
        INativeMatrix<Double>^ _NCIparameters = _Cparameters->getFeature (NativeFeature::Value);
        CDoubleMatrix^ _NCparameters = safe_cast<CDoubleMatrix^>(_NCIparameters);
        std::vector<std::vector<QuantLib::Real> >& _parameters = static_cast<std::vector<std::vector<QuantLib::Real> >&> (_NCparameters->GetReference ());
        _ppParametricExerciseAdapter = new boost::shared_ptr<QuantLib::ParametricExerciseAdapter> (new QuantLib::ParametricExerciseAdapter ( _exercise,  _parameters ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cexercise != nullptr) _Cexercise->Unlock();
        if (_Cparameters != nullptr) _Cparameters->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::CParametricExerciseAdapter (boost::shared_ptr<QuantLib::ParametricExerciseAdapter>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phParametricExerciseAdapter = NULL;
#endif
	_ppParametricExerciseAdapter = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::CParametricExerciseAdapter (QuantLib::ParametricExerciseAdapter& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phParametricExerciseAdapter = NULL;
#endif
	_ppParametricExerciseAdapter = new boost::shared_ptr<QuantLib::ParametricExerciseAdapter> (&childNative);
    
    _ParametricExerciseAdapterOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::CParametricExerciseAdapter (CParametricExerciseAdapter^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phParametricExerciseAdapter = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppParametricExerciseAdapter = new boost::shared_ptr<QuantLib::ParametricExerciseAdapter> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::CParametricExerciseAdapter (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phParametricExerciseAdapter = NULL;
#endif
	if (!t->IsSubclassOf(CParametricExerciseAdapter::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::CParametricExerciseAdapter (QuantLib::Handle<QuantLib::ParametricExerciseAdapter>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phParametricExerciseAdapter = &childNative;
	_ppParametricExerciseAdapter = &static_cast<boost::shared_ptr<QuantLib::ParametricExerciseAdapter>>(childNative.currentLink());
    
    _ParametricExerciseAdapterOwner = owner;
}
Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::CParametricExerciseAdapter (QuantLib::Handle<QuantLib::ParametricExerciseAdapter> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phParametricExerciseAdapter = &childNative;
	_ppParametricExerciseAdapter = &static_cast<boost::shared_ptr<QuantLib::ParametricExerciseAdapter>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::CParametricExerciseAdapter (QuantLib::ParametricExerciseAdapter childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phParametricExerciseAdapter = NULL;
#endif
	_ppParametricExerciseAdapter = new boost::shared_ptr<QuantLib::ParametricExerciseAdapter> (new QuantLib::ParametricExerciseAdapter (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::~CParametricExerciseAdapter ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppParametricExerciseAdapter != NULL)
    {
	    delete _ppParametricExerciseAdapter;
        _ppParametricExerciseAdapter = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::!CParametricExerciseAdapter ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppParametricExerciseAdapter != NULL)
    {
	    delete _ppParametricExerciseAdapter;
    }
}
QuantLib::ParametricExerciseAdapter& Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::GetReference ()
{
    if (_ppParametricExerciseAdapter == NULL) throw gcnew NativeNullException ();
	return **_ppParametricExerciseAdapter;
}
boost::shared_ptr<QuantLib::ParametricExerciseAdapter>& Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::GetShared ()
{
    if (_ppParametricExerciseAdapter == NULL) throw gcnew NativeNullException ();
	return *_ppParametricExerciseAdapter;
}
QuantLib::ParametricExerciseAdapter* Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::GetPointer ()
{
    if (_ppParametricExerciseAdapter == NULL) throw gcnew NativeNullException ();
	return &**_ppParametricExerciseAdapter;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ParametricExerciseAdapter>& Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::GetHandle ()
{
	if (_phParametricExerciseAdapter == NULL)
	{
		_phParametricExerciseAdapter = new Handle<QuantLib::ParametricExerciseAdapter> (*_ppParametricExerciseAdapter);
	}
	return *_phParametricExerciseAdapter;
}
#endif
bool Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::HasNative () 
{
	return (_ppParametricExerciseAdapter != NULL);
}

Boolean Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::Exercise (Cephei::QL::Models::Marketmodels::ICurveState^ currentState)
{
    CCurveState^ _CcurrentState;
    try
    {
        _CcurrentState = safe_cast<CCurveState^> (currentState);
        _CcurrentState->Lock();
        QuantLib::CurveState& _currentState = static_cast<QuantLib::CurveState&> (_CcurrentState->GetReference ()); 
    	bool _rv = (bool)(*_ppParametricExerciseAdapter)->exercise ( _currentState );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurrentState != nullptr) _CcurrentState->Unlock();
    }
}
Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::ExerciseTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppParametricExerciseAdapter)->exerciseTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::IParametricExerciseAdapter^ Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::NextStep (Cephei::QL::Models::Marketmodels::ICurveState^ currentState)
{
    CCurveState^ _CcurrentState;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CcurrentState = safe_cast<CCurveState^> (currentState);
        _CcurrentState->Lock();
        QuantLib::CurveState& _currentState = static_cast<QuantLib::CurveState&> (_CcurrentState->GetReference ()); 
    	(*_ppParametricExerciseAdapter)->nextStep ( _currentState );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurrentState != nullptr) _CcurrentState->Unlock();
    }
}
Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::RelevantTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppParametricExerciseAdapter)->relevantTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::IParametricExerciseAdapter^ Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppParametricExerciseAdapter)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Callability::IParametricExerciseAdapter^ Cephei::QL::Models::Marketmodels::Callability::CParametricExerciseAdapter_Factory::Create (Cephei::QL::Models::Marketmodels::Callability::IMarketModelParametricExercise^ exercise, Cephei::IMatrix<Double>^ parameters)
{
    return gcnew CParametricExerciseAdapter ( exercise,  parameters);
}
